package OuterGameParts;

import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.ExportException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;

import InnerGameParts.CannonBall;
import InnerGameParts.PirateShip;
import InnerGameParts.RemoteList;
import InnerGameParts.RemoteListServer;
import InnerGameParts.Wind;

//import your stuff.
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;


public class GameClient extends JFrame{


	/**
	 * The crap default Id.
	 */
	private static final long serialVersionUID = 1L;
	private RemoteList<PirateShip> myShips;
	private RemoteList<CannonBall> myBalls;
	private RemoteList<Wind> myWind;
	private PirateShip myShip;
	private GamePanel myScreen;
	private AudioPlayer myBackgroundPlayer;



	public GameClient(final RemoteList<PirateShip> theShips,
			final RemoteList<CannonBall> theBalls,
			final RemoteList<Wind> theWind) throws IOException{



		//this.setPreferredSize( new Dimension(1000, 1000));

		intiFullScreen();
		//createBackgroundMusic();
		this.myShips = theShips;
		this.myBalls = theBalls;
		this.myWind = theWind;

		//creates the users ship
		myShip = new PirateShip();
		myShips.add(myShip);

		//creates the screen objects TODO fix rendering thing
		myScreen = new GamePanel(this, this.myShips, this.myBalls, this.myWind, myShip);

		this.add(myScreen);

		//packs the form and then sets allows the thing to 
		this.pack();
		this.setVisible(true);
		new Thread(myScreen).start();

	}

	private void createBackgroundMusic(){

		//make a new AudioPlayer.
		myBackgroundPlayer = AudioPlayer.player;
		AudioStream  myBackgroundMusic;
		ContinuousAudioDataStream myLoop = null;
		//use a try block in case the file doesnt exist.
		try {
			myBackgroundMusic = new AudioStream(new FileInputStream("pirateMusic.mid"));

			AudioData myStream =  myBackgroundMusic.getData();
			
			myLoop = new ContinuousAudioDataStream(myStream);
		}
		catch(IOException error) {

			error.printStackTrace();

		}


		if(myLoop != null){
			// play background music.
			myBackgroundPlayer.start(myLoop);
		}

	}




	/**
	 * Create the full screen mode
	 */
	private void intiFullScreen(){

		GraphicsDevice gd;

		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();


		gd = ge.getDefaultScreenDevice();


		this.setUndecorated(true);
		this.setIgnoreRepaint(true);// have to first get rendering to the screen to work out correctly first
		this.setResizable(false);

		if(!gd.isFullScreenSupported()){

			System.exit(0);

		}
		gd.setFullScreenWindow(this);

		/*
		 * Have to get the bound of the screen later to match it up correctly.
		 */




	}


	public void closeClient(){

		try {
			myShips.remove(myShip);
		} catch (RemoteException e) {
			e.printStackTrace();
		} // causes the players ship to dissapare.
		System.exit(0);

	}
}